package selevert;
import selevert.All;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.util.*;
public class AllFeatures {


    HashMap select() {
        String sql = "select *from one o  INNER JOIN two t  ON o.id  = t.id ";
        ResultSet res = All.select(sql);
        HashMap<Integer, HashMap<String, String>> resultHashMap = new HashMap<>();
        try {
            while (res.next()) {
                int id = res.getInt("id");
                String name = res.getString("name");
                String modal = res.getString("modal");
                HashMap<String, String> innerHashMap = new HashMap<>();
                innerHashMap.put("name", name);
                innerHashMap.put("modal", modal);
                resultHashMap.put(id, innerHashMap);
            }
            for (Map.Entry<Integer, HashMap<String, String>> entry : resultHashMap.entrySet()) {
                int id = entry.getKey();
                HashMap<String, String> innerHashMap = entry.getValue();
                String name = (String) innerHashMap.get("name");
                String modal = innerHashMap.get("modal");
            }
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
        }
        System.out.println(resultHashMap);
        return resultHashMap;

    }

    void add() {
        Scanner scanner = new Scanner(System.in);
        String add_sql = "INSERT INTO info  values (?,?,?)";
        System.out.println("请输入你要添加的id:");
        String id = scanner.next();
        System.out.println("请输入你想添加的品牌:");
        String name = scanner.next();
        System.out.println("请输入你想添加的型号:");
        String modal = scanner.next();
        System.out.println(add_sql);
        All.add(add_sql, id, name, modal);
    }
    void add(int id, String modal,String color) {
        String add_sql = "INSERT INTO info values (?,?,?)";
        All.add(add_sql, String.valueOf(id), modal, color);
    }
    void new_add(int id,String pinpai){
        String add_sql1 = "insert into two values(?,?)";
        All.add(add_sql1, String.valueOf(id),pinpai);
    }

    void select1() {
        System.out.println("请对应以下id输入对应的值:");
        HashMap<Integer, HashMap<String, String>> hashMap = new AllFeatures().select();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你想要查询的id:");
        int num = scanner.nextInt();
        ArrayList<Integer> new_key = new ArrayList<>();
        for (Integer key : hashMap.keySet()) {
            new_key.add(key);
        }
        if (new_key.contains(num)) {

            String sql1 = "select * from info where id=?";
            ResultSet res = All.select(sql1, String.valueOf(num));
            try {
                while (res.next()) {
                    System.out.println(res.getInt("id") + "   " + res.getString("name") + "   " + res.getString("gender"));
                    OneInfo oneInfo = new OneInfo(res.getInt("id"), res.getString("name"), res.getString("gender"));
                    System.out.println(oneInfo);
                }
            } catch (SQLException e) {
                System.out.println("查询失败");
                e.printStackTrace();
            }
        } else {
            System.out.println("你输入的id有误");
            System.out.println("请重新输入:");
        }

    }
    OneInfo select2(int id) {
        int num = id;
        String sql1 = "select *from info i  INNER JOIN two t  ON i.id  = t.id where i.id =? ;";
        ResultSet res = All.select(sql1, String.valueOf(num));
        OneInfo oneInfo = null;
        try {
            while (res.next()) {
                System.out.println(res.getInt("id") + "   " + res.getString("name") + "   " + res.getString("modal"));
                oneInfo = new OneInfo(res.getInt("id"), res.getString("name"), res.getString("modal"));
            }
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
        }
        return oneInfo;
    }


    void updata() {
        System.out.println("请输入你要更新的id:");
        Scanner scanner = new Scanner(System.in);
        String num = scanner.next();
        System.out.println("请输入你要更改的名字是什么:");
        String name = scanner.next();
        String update_sql = "UPDATE info SET name = ?  where id = ?";
        System.out.println(update_sql);
        int res3 = All.updata(update_sql, name, num);
        if (res3 > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
    }
    void updata2(String modal,String name,String id){
        String update_sql = "UPDATE info  i LEFT JOIN two t ON i.id = t.id  set i.modal = ?,t.name  = ? where i.id = ?";
        System.out.println(update_sql);
        int res3 = All.updata(update_sql,modal,name, id);
        System.out.println(modal+name+id);
        System.out.println(res3);
        if (res3 > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
    }

    void delete(int id) {
        String delete_sql = "DELETE FROM info where id =?";
        All.delete(delete_sql, String.valueOf(id));
    }
    void delete1(int id){
        String delete_sql = "DELETE FROM two where id =?";
        All.delete(delete_sql, String.valueOf(id));
    }
}


